//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package cn.lijiajia3515.cairo.core.result.error;

import cn.lijiajia3515.cairo.core.business.Business;
import cn.lijiajia3515.cairo.core.exception.BusinessException;
import cn.lijiajia3515.cairo.core.result.Result;
import cn.lijiajia3515.cairo.core.util.ThrowableUtil;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class ErrorBusinessResult implements Result, Business {
	private String code;
	private String message;
	private List<SimpleExceptionErrorMessage> exception;
	private String trace;


	public ErrorBusinessResult() {
	}

	public ErrorBusinessResult(String code, String message, List<SimpleExceptionErrorMessage> exception, String trace) {
		this.code = code;
		this.message = message;
		this.exception = exception;
		this.trace = trace;
	}

	protected ErrorBusinessResult(ErrorBusinessResult.ErrorBusinessResultBuilder<?, ?> b) {
		this.code = b.code;
		this.message = b.message;
		this.exception = b.exception;
		this.trace = b.trace;
	}

	public static ErrorBusinessResult.ErrorBusinessResultBuilder<?, ?> builder() {
		return new ErrorBusinessResult.ErrorBusinessResultBuilderImpl();
	}

	public ErrorBusinessResult.ErrorBusinessResultBuilder<?, ?> toBuilder() {
		return (new ErrorBusinessResult.ErrorBusinessResultBuilderImpl()).$fillValuesFrom(this);
	}

	@Override
	public String code() {
		return this.code;
	}

	public ErrorBusinessResult code(String code) {
		this.code = code;
		return this;
	}

	@Override
	public String getCode() {
		return Business.super.getCode();
	}

	public ErrorBusinessResult setCode(String code) {
		this.code = code;
		return this;
	}

	@Override
	public String message() {
		return this.message;
	}

	public ErrorBusinessResult message(String message) {
		this.message = message;
		return this;
	}

	public String getMessage() {
		return this.message;
	}

	public ErrorBusinessResult setMessage(String message) {
		this.message = message;
		return this;
	}

	public Business business() {
		return new Business() {
			@Override
			public String code() {
				return code;
			}

			@Override
			public String message() {
				return message;
			}
		};
	}

	public ErrorBusinessResult business(Business business) {
		this.code = business.code();
		this.message = business.message();
		return this;
	}

	public List<SimpleExceptionErrorMessage> exception() {
		return this.exception;
	}

	public ErrorBusinessResult exception(List<SimpleExceptionErrorMessage> exception) {
		this.exception = exception;
		return this;
	}

	public List<SimpleExceptionErrorMessage> getException() {
		return this.exception;
	}

	public ErrorBusinessResult setException(List<SimpleExceptionErrorMessage> exception) {
		this.exception = exception;
		return this;
	}

	public String trace() {
		return this.trace;
	}

	public ErrorBusinessResult trace(String trace) {
		this.trace = trace;
		return this;
	}

	public String getTrace() {
		return this.trace;
	}


	public ErrorBusinessResult setTrace(String trace) {
		this.trace = trace;
		return this;
	}

	private static final class ErrorBusinessResultBuilderImpl extends ErrorBusinessResult.ErrorBusinessResultBuilder<ErrorBusinessResult, ErrorBusinessResult.ErrorBusinessResultBuilderImpl> {
		private ErrorBusinessResultBuilderImpl() {
		}

		protected ErrorBusinessResult.ErrorBusinessResultBuilderImpl self() {
			return this;
		}

		public ErrorBusinessResult build() {
			return new ErrorBusinessResult(this);
		}
	}

	public abstract static class ErrorBusinessResultBuilder<C extends ErrorBusinessResult, B extends ErrorBusinessResult.ErrorBusinessResultBuilder<C, B>> {
		private String code;
		private String message;
		private List<SimpleExceptionErrorMessage> exception;
		private String trace;

		public ErrorBusinessResultBuilder() {
		}

		protected B $fillValuesFrom(C instance) {
			$fillValuesFromInstanceIntoBuilder(instance, this);
			return this.self();
		}

		private static void $fillValuesFromInstanceIntoBuilder(ErrorBusinessResult instance, ErrorBusinessResult.ErrorBusinessResultBuilder<?, ?> b) {
			b.code(instance.code);
			b.message(instance.message);
			b.exception(instance.exception);
			b.trace(instance.trace);
		}

		protected abstract B self();

		public abstract C build();

		public B code(String code) {
			this.code = code;
			return this.self();
		}

		public B message(String message) {
			this.message = message;
			return this.self();
		}

		public B business(Business business) {
			this.code = business.getCode();
			this.message = business.message();
			return this.self();
		}

		public B exception(List<SimpleExceptionErrorMessage> exception) {
			this.exception = exception;
			return this.self();
		}

		public B trace(String trace) {
			this.trace = trace;
			return this.self();
		}

		public B exceptions(List<Throwable> causes) {
			this.exception = causes.stream()
				.map(x -> SimpleExceptionErrorMessage.builder().bean(x.getClass().getName()).message(x.getMessage()).build())
				.collect(Collectors.toList());
			causes.stream().findFirst().map(ThrowableUtil::extractTrace).ifPresent(d -> this.trace = d);
			return this.self();
		}
	}


}
